<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 密码解密（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一段“密文”字符串 s&#xff0c;其中字符都是经过“密码本”映射的&#xff0c;现需要将“密文”解密并输出。</p> 
<p>映射的规则&#xff08;&#39;a&#39; ~ &#39;i&#39;&#xff09;分别用&#xff08;&#39;1&#39; ~ &#39;9&#39;&#xff09;表示&#xff1b;&#xff08;&#39;j&#39; ~ &#39;z&#39;&#xff09;分别用&#xff08;&#34;10*&#34; ~ &#34;26*&#34;&#xff09;表示。</p> 
<p>约束&#xff1a;映射始终唯一。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>“密文”字符串</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>明文字符串</p> 
<p></p> 
<h4>备注</h4> 
<p>翻译后的文本长度在100以内</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">20*19*20*</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">tst</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题有多种解题思路&#xff0c;我的思路是利用字符串替换操作。</p> 
<p>即将如下密文和明文进行按照顺序依次映射替换</p> 
<ul><li>&#34;26*&#34; → &#34;z&#34;</li><li>&#34;25*&#34; → &#34;y&#34;</li><li>...</li><li>&#34;10*&#34; → &#34;j&#34;</li><li>&#34;9&#34; → &#34;i&#34;</li><li>...</li><li>&#34;1&#34; → &#34;a&#34;</li></ul> 
<p>这里优先将从“26*” ~ &#34;10*&#34; 开始映射替换&#xff0c;比如题目用例&#xff1a;</p> 
<blockquote> 
 <p>20*19*20*</p> 
</blockquote> 
<p>如果优先进行 &#34;1&#34; ~ “9” 密文的映射替换&#xff0c;则会产生错误答案。</p> 
<p>更多实现细节&#xff0c;请看代码。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

// 输入处理
void (async function () {
  let s &#61; await readline();

  for (let i &#61; 26; i &gt;&#61; 1; i--) {
    const key &#61; i &#43; (i &gt; 9 ? &#34;*&#34; : &#34;&#34;);
    const val &#61; String.fromCharCode(97 &#43; i - 1);

    s &#61; s.replaceAll(key, val);
  }

  console.log(s);
})();
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String s &#61; sc.nextLine();

    for (int i &#61; 26; i &gt;&#61; 1; i--) {
      String key &#61; i &#43; (i &gt; 9 ? &#34;\\*&#34; : &#34;&#34;);
      String val &#61; String.valueOf((char) (&#39;a&#39; &#43; i - 1));
      s &#61; s.replaceAll(key, val);
    }

    System.out.println(s);
  }
}
</code></pre> 
<p> </p> 
<h4 style="background-color:transparent;">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
s &#61; input()

for i in range(26, 0, -1):
    key &#61; str(i)
    if i &gt; 9:
        key &#43;&#61; &#34;*&#34;

    val &#61; chr(97 &#43; i - 1)

    s &#61; s.replace(key, val)

print(s)</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

char* replace(char* s, char* old, char* new) {
    char* sub &#61; strstr(s, old);

    if(sub &#61;&#61; NULL) {
        return s;
    }

    char* res &#61; (char*) calloc(100, sizeof(char));

    while (sub !&#61; NULL) {
        sub[0] &#61; &#39;\0&#39;;
        strcat(res, s);
        strcat(res, new);

        s &#61; sub &#43; strlen(old);
        sub &#61; strstr(s, old);
    }

    strcat(res, s);

    return res;
}

int main() {
    char* s &#61; (char*) calloc(100, sizeof(char));
    gets(s);

    for (int i &#61; 26; i &gt;&#61; 1; i--) {
        char key[4];
        sprintf(key, &#34;%d&#34;, i);

        if(i &gt; 9) {
            strcat(key, &#34;*&#34;);
        }

        char val[2];
        sprintf(val, &#34;%c&#34;, 97 &#43; i - 1);

        s &#61; replace(s, key, val);
    }

    puts(s);

    return 0;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>